package com.wby.store.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wby.store.common.utils.PageUtils;
import com.wby.store.common.utils.Query;
import com.wby.store.product.dao.CategoryDao;
import com.wby.store.product.entity.CategoryEntity;
import com.wby.store.product.service.CategoryService;
import com.wby.store.product.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryVO> listByTree() {
        List<CategoryEntity> categoryEntities = this.getBaseMapper().selectList(null);
        List<CategoryVO> categoryVOS = categoryEntities.stream().map(
                t -> {
                    CategoryVO categoryVO = new CategoryVO();
                    BeanUtils.copyProperties(t, categoryVO);
                    return categoryVO;
                }
        ).collect(Collectors.toList());
        return categoryVOS.stream()
                .filter(t -> t.getParentCid() == 0)
                .peek(t -> t.setChildCategory(getTree(t, categoryVOS)))
                .sorted(Comparator.comparing(CategoryVO::getSort))
                .collect(Collectors.toList());
    }

    @Override
    public List<CategoryVO> getByTree(){
        List<CategoryEntity> categoryEntities = this.getBaseMapper().selectList(null);
        List<CategoryVO> collect = categoryEntities.stream().map(
                t -> {
                    CategoryVO categoryVO = new CategoryVO();
                    BeanUtils.copyProperties(t, categoryVO);
                    return categoryVO;
                }
        ).collect(Collectors.toList());
        return collect.stream()
                .filter(t->t.getParentCid()==0)
                .peek(t->t.setChildCategory(getChildTree(t,collect)))
                .collect(Collectors.toList());

    }

    private List<CategoryVO> getChildTree(CategoryVO categoryVO, List<CategoryVO> collect) {
        List<CategoryVO> collect1 = collect.stream().filter(t -> t.getParentCid().equals(categoryVO.getCatId()))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect1)) {
            return new ArrayList<>();
        }
        return collect1.stream().peek(t -> t.setChildCategory(getChildTree(t, collect)))
                .collect(Collectors.toList());
    }

    public List<CategoryVO> getTree(CategoryVO root, List<CategoryVO> categoryVOS) {
        List<CategoryVO> collect =
                categoryVOS.stream().filter(t -> t.getParentCid().equals(root.getCatId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return new ArrayList<>();
        }
        List<CategoryVO> result = collect.stream()
                .peek(t -> t.setChildCategory(getTree(t, categoryVOS)))
                .sorted((o1,o2)->(o1.getSort()==null?0:o1.getSort())-(o2.getSort()==null?0:o2.getSort()))
                .collect(Collectors.toList());
        return result;
    }

}
